<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  global $text, $req;

  $name = substr($text, strpos($text, '<h1><b>') + 7);
  $name = substr($name, 0, strpos($name, '</b>'));

  echo "<strong>$name</strong><br/>";
  if (empty($req["submod"])) $smod = "train";
  else $somd = $req["submod"];

  if (!empty($req["xcount"]))
  {
    $traini = travian_return_train_info($text, $req["id"], True);
    $traini = $traini["form"];
    $post = array(
      "id"  => $req["id"],
      "z"   => $traini["z"],
      "a"   => $traini["a"]
    );
    for ($i = 0; $i < $req["xcount"]; $i++)
    {
      $y = $i + 1;
      $post[$traini[$i]["fname"]] = process_number($req["i$y"]);
    }
    $temp = http_post("http://{$_SESSION["serv"]}/build.php", $post);
    $text = $temp["body"];
  }
  
  $traini = travian_return_train_info($text, $req["id"], True);
  if ($smod == "train")
  {
    $temp = $traini["form"];
    print_link("main.php?mod=details_build&amp;id={$req["id"]}&amp;submod=atrain".ps(), "Already in training", "<br/>");
    $x = 0;
    $data = NULL;
    foreach ($temp as $key => $value)
    {
      if (is_numeric($key))
      {
        $x++;
        $data .= "{$value["name"]}[{$value["exist"]}-{$value["max"]}]<br/>";
        for ($i = 0; $i <= 5; $i++)
        {
          if ($i != 5) $data .= $value["req"][$i]." ";
          else $data .= pdate($value["req"][$i]);
          if (($i == 3) || ($i == 5)) $data .= "<br/>";
        }
        $data .= '<input name="i'.$x.'" type="text" value="0"/><br/>';
        $pfields["i$x"] = '$(i'.$x.')';
      }
    }
    $pfields["mod"]    = "details_build";
    $pfields["id"]     = $req["id"];
    $pfields["submod"] = "";
    $pfields["xcount"] = $x;
    print_form_top("main.php".ps('?'), $pfields, "get");
    echo $data;
    print_form_bottom("Train");
  }
  else
  {
    print_link("main.php?mod=details_build&amp;id={$req["id"]}&amp;submod=".ps(), "Train", "<br/>");
    $temp = $traini["train"];
    if (!empty($temp))
    {
      foreach ($temp as $key => $value)
      {
        if (is_numeric($key)) echo "[{$value["count"]}] {$value["name"]} - ".pdate($value["time"])."<br/>";
      }
      echo "Next unit finished in:<br/>".pdate($temp["next"])."<br/><br/>";
    }
    else echo "No units currently in training<br/><br/>";
  }

